package com.hyxt.provider;

import com.alibaba.druid.pool.DruidPooledConnection;
import com.alibaba.dubbo.config.annotation.Service;
import com.hyxt.api.ArcgisService;
import com.hyxt.utils.DBPoolConnection;
import com.hyxt.utils.HttpClientUtil;
import oracle.jdbc.OracleTypes;


import java.sql.*;
/**
 *
 * @author wxf
 * @version v1.0
 * @Description 查询地址
 * @Date: Create in 12:40 2017/11/22
 * @Modifide By:
 **/
@Service(interfaceClass = ArcgisService.class,version = "1.0.0")
public class GisServiceImpl implements ArcgisService {
    @Override
    public String getaddress(double lon, double lat, String type) {
        // 初始化地址
        String address = "未准确定位";
        //判断查询类型
        if(type=="1"){
            //创建一个连接池
            //DBPoolConnection dbp =  new DBPoolConnection();
            DBPoolConnection  dbp = DBPoolConnection.getInstance();
            DruidPooledConnection connection = null;
            CallableStatement c = null;
            // PreparedStatement c=null;
            try {
                connection =  dbp.getConnection();
                String sql = "{call p_getexactlocation(?,?,?,?,?,?,?,?)}";
                c= connection.prepareCall(sql);
                // c = connection.prepareStatement(sql);
                c.setDouble(1,lon);
                c.setDouble(2, lat);
                Float outlon = 0f;
                Float outlat = 0f;
                Float distance = 0f;
                Float course = 0f;
                Float tilt = 0f;
                c.registerOutParameter(3, OracleTypes.FLOAT);
                c.registerOutParameter(4, OracleTypes.FLOAT);
                c.registerOutParameter(5, OracleTypes.FLOAT);
                c.registerOutParameter(6, OracleTypes.FLOAT);
                c.registerOutParameter(7, OracleTypes.FLOAT);
                c.registerOutParameter(8, oracle.jdbc.OracleTypes.VARCHAR, 500);
                c.executeUpdate();
                outlon = c.getFloat(3);
                outlat = c.getFloat(4);
                distance = c.getFloat(5);
                course = c.getFloat(6);
                tilt = c.getFloat(7);
                address = c.getString(8);
                address += computeBearing(course);
                address += Double.valueOf(distance).intValue() + "米";
            } catch (SQLException e) {
                e.printStackTrace();
            }finally {
                if(c!=null){
                    try {
                        c.close();
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                }
                if(connection!=null){
                    try {
                        connection.close();
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                }
            }


        }else{
        //从高德地图去查询
            String url="http://restapi.amap.com/v3/geocode/regeo?location="+lon+","+lat+"&key=fb0bf63ae8a660095e3aee5495657610&radius=0";
            address =  HttpClientUtil.sendGet(url);
        }
        return address;
    }

    /**
     * @Description:计算方向，把360的形式转化为中文信息
     * @param i
     *            角度
     * @return String 中文方向
     * @throws Exception
     */
    public String computeBearing(double i) {
        String result = "";
        // 根据360度的方向判断是哪个方向
        if (i > 360) {
            result = "附近";
            // 8个方向把360度分8份
        } else if (i >= 0 && i < 22.5) {
            result = "正北方向";
            // 8个方向把360度分8份
        } else if (i >= 22.5 && i < 67.5) {
            result = "东北方向";
            // 8个方向把360度分8份
        } else if (i >= 67.5 && i < 112.5) {
            result = "正东方向";
            // 8个方向把360度分8份
        } else if (i >= 112.5 && i < 157.5) {
            result = "东南方向";
            // 8个方向把360度分8份
        } else if (i >= 157.5 && i < 202.5) {
            result = "正南方向";
            // 8个方向把360度分8份
        } else if (i >= 202.5 && i < 247.5) {
            result = "西南方向";
            // 8个方向把360度分8份
        } else if (i >= 247.5 & i < 292.5) {
            result = "正西方向";
            // 8个方向把360度分8份
        } else if (i >= 292.5 && i < 337.5) {
            result = "西北方向";
            // 8个方向把360度分8份
        } else if (i >= 337.5 && i <= 360) {
            result = "正北方向";
        }
        return result;
    }
}
